package com.up.vms.interfaces.rest.controller;

import com.up.vms.interfaces.rest.request.BannerIdRequest;
import com.up.vms.interfaces.rest.request.CategoryIdRequest;
import com.up.vms.interfaces.rest.response.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.up.vms.application.service.BannerApplicationService;
import com.up.vms.domain.module.BannerDTO;
import com.up.vms.domain.vm.entity.Banner;
import com.up.vms.domain.vm.enums.BannerStatus;
import com.up.vms.infrastructure.constants.EmpAPIConstants;
import com.up.vms.infrastructure.mvc.BaseResponse;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import com.up.vms.interfaces.rest.request.BannerRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "轮播图管理", produces = "application/json")
@RequestMapping(EmpAPIConstants.API_ROOT)
@Slf4j
public class BannerController {

    @Autowired
    private BannerApplicationService bannerApplicationService;

    @RequestMapping(value = "/banner/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前类型下的活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "页码", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页记录数", dataType = "Integer")
    })
    @ResponseBody
    public BaseResponse getBannerList(
            @RequestParam(value = "bannerName", required = false) String bannerName,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "index") Integer index,
            @RequestParam(value = "size") Integer size) {
        GeneRespDTO<HashMap> geneRespDTO = null;
        CommonResponse campaignListResponse = new CommonResponse();
        BannerDTO bannerDTO = new BannerDTO();

        try {
            if (CommonUtils.isNotNull(index)) {
                bannerDTO.setPage(index);
            }
            if (CommonUtils.isNotNull(size)) {
                bannerDTO.setSize(size);
            }
            if (CommonUtils.isNotNull(bannerName)) {
                bannerDTO.setBannerName(bannerName);
            }
            if (CommonUtils.isNotNull(status)) {
                bannerDTO.setStatus(status);
            }

            geneRespDTO = bannerApplicationService.getBannerList(bannerDTO);

            campaignListResponse.setData(geneRespDTO.getData());
            campaignListResponse.setRespCode(new RespCode(ReturnCode.SUCCESS));

        } catch (Exception e) {
            log.error("获取轮播图列表失败：{}", e.getMessage(), e.getCause());
            campaignListResponse.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return campaignListResponse;
    }

    @RequestMapping(value = "/banner/delete", method = RequestMethod.POST)
    public BaseResponse delete(@RequestBody BannerIdRequest request) {

        CommonResponse response = new CommonResponse();
        try {
            List<String> bannerIdList = new ArrayList<>();

            if (CommonUtils.isNull(request.getBannerId())) {
                throw new Exception("bannerId is null");
            }

            String bannerId = request.getBannerId();
            if (bannerId.contains(",")) {
                String[] cs = bannerId.split(",");
                for (String s : cs) {
                    bannerIdList.add(s);
                }
            } else{
                bannerIdList.add(bannerId);
            }

            GeneRespDTO geneRespDTO = bannerApplicationService.delete(bannerIdList);

            response.setRespCode(geneRespDTO.getRespCode());
            response.setData(geneRespDTO.getData());
        } catch (Exception ex) {
            log.error(ex.getMessage());
            response.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR.getCode(), ex.getMessage()));
        }

        return response;

    }

    @RequestMapping(value = "/banner/save", method = RequestMethod.POST)
    public BaseResponse saveBanner(@RequestBody BannerRequest request) {
        CommonResponse response = new CommonResponse();
        try {
            Banner banner = convertFromRequest(request);

            GeneRespDTO geneRespDTO = bannerApplicationService.saveBanner(banner);

            response.setRespCode(geneRespDTO.getRespCode());
            response.setData(geneRespDTO.getData());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            response.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return response;
    }

    private Banner convertFromRequest(BannerRequest request) {
        Banner banner = new Banner();

        if (CommonUtils.isNotNull(request.getBannerId())) {
            banner.setBannerId(request.getBannerId());
        }

        if (CommonUtils.isNotNull(request.getBannerName())) {
            banner.setBannerName(request.getBannerName());
        }

        banner.setBannerImage(request.getBannerImage());

        if (CommonUtils.isNotNull(request.getForwardType())) {
            banner.setForwardType(request.getForwardType());
        }

        banner.setForwardUrl(request.getForwardUrl());
        banner.setSorting(request.getSorting());

        if (CommonUtils.isNotNull(request.getStatus())) {
            banner.setStatus(request.getStatus());
        } else {
            banner.setStatus(BannerStatus.CLOSE.code);
        }

        return banner;
    }
}
